package com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.dao;

import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.entity.ImportPaymentFile;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author Lianhong
 * @since 2021/11/30
 */
@Repository
public class ImportPaymentFileDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String selectImportPaymentFileSql = "SELECT file FROM e_import_payment_file WHERE entryId = ?";

    public String findByEntryId(String entryId) {
        return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(selectImportPaymentFileSql,
                new Object[]{entryId}, String.class);
    }

    private String existsImportPaymentFileSql = "SELECT count(1) FROM e_import_payment_file WHERE entryId = ?";

    public boolean exists(String entryId) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsImportPaymentFileSql, new Object[]{entryId}, Integer.class);
        return count != null && count > 0;
    }

    private String insertExportProceedsFileSql = "INSERT INTO e_import_payment_file (entryId, file, storageTime) VALUES (:entryId, :file, :storageTime)";

    public void save(ImportPaymentFile importPaymentFile) {
        importPaymentFile.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.insertExportProceedsFileSql, new BeanPropertySqlParameterSource(importPaymentFile));
    }
}
